﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;
using Top.Web;
using Top.Web.Api;
using Top.Web.Api.Filters;
using Top.Core;
using Top.PointStore.Business;
using Top.PointStore.Web.Filters;
//using Top.PointStore.Filters;

namespace Top.PointStore.Controllers
{
    /// <summary>
    /// 商品推广
    /// </summary>
    [Provider]
    public class ProductsExpandController : ApiBase
    {
        //
        // GET: /ProductsExpand/

        #region admin
        /// <summary>获取数据列表
        /// </summary>
        /// <returns></returns>
        [Login]
        public async Task<object> GetList(int pageIndex = 1, int pageSize = 10)
        {
            if (!User.IsAuthenticated)
            {
                throw new WebApiException(ExceptionCode.NotLogin, "请先登录");
            }
            return await ProductsExpandManager.GetList(Pager.Create(pageIndex, pageSize));
        }


        /// <summary>得到一条记录
        /// </summary>
        /// <returns></returns>
        [Login]
        public async Task<object> Get(int kid)
        {
            return await ProductsExpandManager.Get(kid);
        }
        #endregion



        /// <summary>保存商品推广信息
        /// </summary>
        /// <returns></returns>
        [Login]
        public async Task<object> Save(string showName, string expandUrl, int kid, string picUrl, int sort)
        {
            var model = new T_ProductsExpand();
            model.Kid = kid;
            model.F_ShowName = showName;
            model.F_ExpandUrl = expandUrl;
            model.F_PicUrl = picUrl;
            model.F_Sort = sort;
            var result = 0;
            if (model.Kid > 0)
            {
                model.F_UpdatedTime = DateTime.Now;
                result = await ProductsExpandManager.Update(model);
            }
            else
            {
                model.F_IsDelete = 0;
                model.F_CreatedTime = DateTime.Now;
                model.F_UpdatedTime = DateTime.Now;
                result = await ProductsExpandManager.Add(model);
            }
            switch (result)
            {
                case -1:
                    throw new WebApiException("商品推广id 有误");
                case -2:
                    throw new WebApiException("商品展示位名称不能为空");
                case -3:
                    throw new WebApiException("推广链接不能为空");
                case -4:
                    throw new WebApiException("图片不能为空");
                default:
                    break;
            }
            return result;
        }

        /// <summary>删除记录
        /// </summary>
        /// <param name="kids"></param>
        /// <returns></returns>
        [Login]
        public async Task<object> Delete(string kids)
        {
            if (string.IsNullOrEmpty(kids))
            {
                throw new WebApiException(ExceptionCode.ParameterError, "编号不能为空！");
            }
            if (kids.Split(',').Length >= 2)
            {
                return await ProductsExpandManager.DeleteList(kids);
            }
            else
            {
                return await ProductsExpandManager.Delete(int.Parse(kids));
            }

        }



    }
}
